<?php
/**
 * User: hunter
 * Date: 2018/6/28
 * Time: 18:21
 */
namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class jsonHtmlEditor extends Field
{
    protected $view = 'laravel-admin.form.jsonHtmlEditor';

    // protected static $css = [
    //     '/vendor/json_edit_schema_html_editor/dist/jsoneditor.min.css',
    // ];

    protected static $js = [
        '/vendor/json_edit_schema_html_editor/dist/jsoneditor.js',
    ];

    public function render()
    {
        $name = $this->formatName($this->column);

        $this->script = <<<EOT
var container = document.getElementById('jsonhtmleditor_{$name}');
var options = {
theme: 'bootstrap3',
disable_collapse: true,
schema: {}
};
console.log(options);

if($(":input[name='{$name}']").val() !=''){
var json =  JSON.parse($(":input[name='{$name}']").val());
}else{
var json =  JSON.parse("{}");
}

var editor = new JSONEditor(container,options);
editor.setValue(json);
editor.on("change",  function() {
  console.log(editor.getValue());
  $(":input[name='{$name}']").val(JSON.stringify(editor.getValue()));
});
EOT;
        return parent::render();
    }
}